import prompt from '@system.prompt';
import router from '@system.router';
import http from '@ohos.net.http';

//轻量级存储
import storage from '@system.storage';


export default {
    data: {
        title: "",
        password:"123",
        name:"YZJ",
        storage:"",
        result:{

        }
    },
    onInit() {

    },
    onAccount(e){
        this.name=e.value
    },
    onPassword(e){
        this.password= e.value
    },
    onLogin(){
        let httpRequest = http.createHttp();
        httpRequest.request(
            "https://www.xdsbty.cn/login",
            {
                method:http.RequestMethod.POST,
                header: {
                    'Content-Type': 'application/json'
                },
                extraData:{
                    "Name":this.name,
                    "Password":this.password,
                },
                readTimeout: 10000,
                connectTimeout: 10000,

            }, (err, data) => {
            if (!err) {
                console.info("YZJ--- "+JSON.stringify(data))

                this.result = JSON.parse(data.result);
                if(this.result.isSuccess==true){
                    router.push({
                        uri:"pages/index/index",
                        params:{
                            user_name:this.name
                        }
                    })
                }
                else{
                    prompt.showToast({
                        message:this.result.message
                    })
                }

                console.info("YZJ--- TOKEN :"+this.result.token)
                //把token放入轻量级缓存
                storage.set({
                    key:"token",
                    value:this.result.token,
                    success(){
                        console.log("CHWxxx----轻量级存储token成功")
                    },
                    fail(data, code){
                        console.log("CHWxxx----轻量级存储token失败,data:"+JSON.stringify(data))
                    }
                });
                storage.set({
                    key:"user_name",
                    value:this.name,
                    success(){
                        console.log("CHWxxx----用户登陆成功")
                    },
                    fail(data,code){
                        console.log("CHWxxx----用户登陆失败")
                    }
                })
                console.info("YZJ---"+this.result.isSuccess)
            } else {
                console.info('YZJ--- error:' + JSON.stringify(err));
                let error = JSON.parse(err)
                prompt.showToast({
                    message:error.message
                })
            }
        })
    },
    onRegisterpage(){
        router.push({
            uri:"pages/register/register"
        })
    }


}
